//    Copyright (C) Kherty.  All rights reserved.

using System;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.Parsing;
using System.Diagnostics;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class FormulaColumnsReference: FormulaDimensionsReference<Column, Row>
    {
        public override void ToggleLock()
        {
            Console.WriteLine("todo");

        }
        internal FormulaColumnsReference(Token t, FullRangeReference<Column, Row> rangeReference, Formula owner) : base(t, rangeReference, owner)
        {
            DimensionReference = new AnchoredColumnReference(rangeReference.Range.TopLeftCell.Column, false);
        }

        public FormulaColumnsReference(Token t, ColumnReference cr, Formula owner)
            : base(t, cr, owner)
        {
            this.RangeReference = new FullColumnRangeReference(cr, cr);
        }


        internal override void Extend(CellAddress address, ResizerType type)
        {
            if (DimensionReference != null)
            {
                if (DimensionReference.Anchor.Index == address.Column)
                {
                    if (this.RangeReference.Range.ColumnSpan != 1)
                        this.RangeReference = new FullColumnRangeReference(DimensionReference, DimensionReference);
                    return;
                }
                ColumnReference c2 = (ColumnReference) DimensionReference.OffsetDim(address - new CellAddress(0, DimensionReference.Anchor.Index));
                RangeReference = new FullColumnRangeReference(DimensionReference, c2);
               // DimensionReference = null;
                return;
            }
                
            int thisColIndex = RangeReference.Range.TopLeftCell.Column.Index;
            int otherColIndex = address.Column;
            RangeReference = new FullColumnRangeReference(new AnchoredColumnReference(Worksheet.Columns[thisColIndex], false),
                new AnchoredColumnReference(Worksheet.Columns[otherColIndex], false));
        }

        internal override void Move(NavigationDirection navigationDirection, bool hold)
        {
            if (hold)
            {
                Column anchor = DimensionReference.Anchor;
                Column c1 = ((FullColumnRange)RangeReference.Range).FirstColumn;
                Column c2 = ((FullColumnRange)RangeReference.Range).LastColumn;
                Column toMove = c1 == anchor ? c2 : c1;
                Cell topCell = toMove.Worksheet[0, toMove.Index];
                topCell = topCell.GetNeighbor(navigationDirection);

                RangeReference = new FullColumnRangeReference(new AnchoredColumnReference(anchor, false), new AnchoredColumnReference(topCell.Column, false));
            }
            else
            {
                DimensionReference = DimensionReference.OffsetDim(GetRelativeAddress(navigationDirection));
                RangeReference = new FullColumnRangeReference(DimensionReference, DimensionReference);
            }
        }
    }
}